package com.jiyun.personal.controller;
import com.jiyun.personal.domain.Personal;
import com.jiyun.personal.service.PersonalService;
import com.jiyun.personal.utils.MySmsUtils;
import com.jiyun.result.Result;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.web.bind.annotation.*;
import java.util.List;
import java.util.Random;


@RestController
@RequestMapping("/personal")
public class PersonalController {
    @Autowired
    private  PersonalService personalService;
    @Value("${appCode}")
    private String appCode;

    //      查询所有
    @GetMapping("/getPersonal")
    public List<Personal> getPersonal(){
        List<Personal> list = personalService.getPersonal();
        return list;
    }
    //      常用地址
    @GetMapping("/address/{passengerId}")
    public String getAddress(@PathVariable("passengerId") String fareAddress){
        return personalService.getAddress(fareAddress);
    }
    //      查询手机号
    @GetMapping("/chaxun/{farePhone}")
    public Personal chaxun(@PathVariable("farePhone") String farePhone){
        return personalService.chaxun(farePhone);
    }
    //      查找初始密码
    @GetMapping("/isOldPasswordCorrect")
    public String isOldPasswordCorrect(String passengerId, String fareStartPassword){
        return personalService.isOldPasswordCorrect(passengerId,fareStartPassword);
    }
    //      修改密码
    @GetMapping("/updatePassword")
    public  Integer updatePassword(String passengerId,String farePassword){
       return personalService.updatePassword(passengerId,farePassword);
    }
    //      修改手机号码
    @GetMapping("/updatePhone")
    public  Integer updatePhone(String passengerId,String farePhone){
        return personalService.updatePhone(passengerId,farePhone);
    }
    //      注册以及验证
    @GetMapping("/register/{farePhone}")
    public Result register(@PathVariable("farePhone") String farePhone){
        //生成随机验证码
        Random random = new Random();
        //生成4位随机数
        String code = String.valueOf(random.nextInt(9000) + 1000);
        try {
            //发送验证码
            MySmsUtils.sendMessage(farePhone,code,appCode);
        }
        catch (Exception e)
        {
            throw new RuntimeException(e);
        }
        //向redis中存一份
        //redisTemplate.opsForValue().set(farePhone,code,3, TimeUnit.MINUTES);
        return Result.ok(code);
    }
    //      修改状态
    @GetMapping("/updateStatus2/{id}")
    public Integer updateStatus2(@PathVariable Integer id){
        return personalService.updateStatus2(id);
    }
    @GetMapping("/updateStatus3/{id}")
    public Integer updateStatus3(@PathVariable Integer id){
        return personalService.updateStatus3(id);
    }
    @GetMapping("/updateStatus4/{id}")
    public Integer updateStatus4(@PathVariable Integer id){
        return personalService.updateStatus4(id);
    }
    @GetMapping("/updateStatus5/{id}")
    public Integer updateStatus5(@PathVariable Integer id){
        return personalService.updateStatus5(id);
    }
    @GetMapping("/updateStatus6/{id}")
    public Integer updateStatus6(@PathVariable Integer id){
        return personalService.updateStatus6(id);
    }
    @GetMapping("/updateStatus7/{id}")
    public Integer updateStatus7(@PathVariable Integer id){
        return personalService.updateStatus7(id);
    }
}
